<?php


namespace app\common\service;


use app\common\model\OrderMemberInfo;

class Verification
{
    //验证身份证号 姓名
    public static function verificationInfo($username,$id_number,$phone){
        foreach ($username as $key => $value){
            if (empty($value)){
                return [
                    'status' => false,
                    'msg'    => "当前姓名不能为空"
                ];
            }
            if (empty($id_number[$key]) ||!preg_match('/^\d{17}[0-9xX]$/', $id_number[$key]) ){
                return [
                    'status' => false,
                    'msg'    => "身份证号格式不正确".$id_number[$key]
                ];
            }
            if(!preg_match("/^1[34578]\d{9}$/", $phone[$key])){
                return [
                    'status' => false,
                    'msg'    => "手机号格式不正确".$phone[$key]
                ];
            }
        }
        foreach ($username as $k =>$v){
            $res = self::sendVerification($v,$id_number[$k]);
            if (!$res['status']){
                return $res;
            }
        }
        return [
            'status' => true,
            'msg'    => "成功"
        ];
        //
//        dump($value);
//        dump($id_number[$key]);
    }
    //发送验证请求
    public static function sendVerification($userName,$identifyNum){
        $host = "https://safrvcert.market.alicloudapi.com";
        $path = "/safrv_2meta_id_name/";
        $method = "GET";
        $appcode = "38a5108e624c4a2a9cae4648ccd5e73a";
        $userId = "1045298080122337";
        $verifykey = "IVVvLr8MBKhI3O";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "__userId=".$userId."&customerID=customerID&identifyNum=".$identifyNum."&userName=".$userName."&verifyKey=".$verifykey."&identifyNumMd5=identifyNumMd5";
        $bodys = "";
        $url = $host . $path . "?" . $querys;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $response = curl_exec($curl);
        curl_close($curl);
        $res = json_decode($response,true);
        if ($res['code'] ==200){
            if ($res['value']['bizCode'] == 0){
                return  [
                    'status' => true,
                    'msg'   => "成功"
                ];
            }
            return [
                'status' => false,
                'msg'    =>$res['value']['message']
            ];
        }else{
            return [
                'status' => false,
                'msg'    => $res['message']
            ];
        }
//        return $response;
    }
    //添加身份证号 姓名
    public static function saveInfo($username,$id_number,$phone,$order_id,$ordersGoods){
        foreach ($ordersGoods as $key =>$value){
            $data = new OrderMemberInfo();
            $data->username = $username[$key];
            $data->id_number = $id_number[$key];
            $data->phone = $phone[$key];
            $data->order_id = $order_id;
            $data->order_goods_id = $value['id'];
            $data->save();
        }

    }
}